//
//  InputPhoneViewController.m
//  location
//
//  Created by Dongxiang Xu on 2025/5/16.
//

#import "InputPhoneViewController.h"
#import "VerCodeViewController.h"


@interface InputPhoneViewController ()<UITextFieldDelegate>

@property (nonatomic,strong) UILabel *code;
@property (nonatomic,strong) UIButton *next;
@property (nonatomic,strong) UITextField *textField;

@end

@implementation InputPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = WhiteColor;
    self.navigationItem.hidesBackButton = YES;
    [self initView];
}

- (void)initView
{
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, STATUSHEIGHT+16, SCREEN_WIDTH, 26)];
    title.font = REAL_MEDIUM_FONT(17);
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = RealBlackColor;
    title.text = @"输入手机号";
    [self.view addSubview:title];
    
    UIButton *close = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-50, STATUSHEIGHT+12, 30, 30)];
    [close setImage:[UIImage imageNamed:@"map_close"] forState:UIControlStateNormal];
    [self.view addSubview:close];
    [[close rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    UILabel *subTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, title.maxY+13, SCREEN_WIDTH, 18)];
    subTitle.font = REAL_MEDIUM_FONT(13);
    subTitle.textAlignment = NSTextAlignmentCenter;
    subTitle.textColor = [GraryHeavyLightColor colorWithAlphaComponent:0.6];
    subTitle.text = @"点击下一步你会收到一个4位数的验证码";
    [self.view addSubview:subTitle];
    
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-210)/2, subTitle.maxY+20, 210, 210)];
    img.image = [UIImage imageNamed:@"input_code"];
    [self.view addSubview:img];
    
    UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(img.frame)+22, SCREEN_WIDTH-60, 60)];
    inputView.backgroundColor = RealWhiteColor;
    inputView.layer.masksToBounds = YES;
    [inputView.layer setCornerRadius:18];
    [self.view addSubview:inputView];
    
    UILabel *input = [[UILabel alloc] initWithFrame:CGRectMake(12, 6, 62, 21)];
    input.font = REAL_MEDIUM_FONT(12);
    input.textColor = GraryTooHeavyColor;
    input.text = @"输入手机号";
    [inputView addSubview:input];
    
    _code = [[UILabel alloc] initWithFrame:CGRectMake(12, input.maxY, 30, 27)];
    _code.font = REAL_MEDIUM_FONT(16);
    _code.textColor = ThemeDark2;
    _code.text = @"+86";
    [inputView addSubview:_code];
    
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_code.frame)+8, input.maxY, inputView.width-CGRectGetMaxX(_code.frame)-20, _code.height)];
    _textField.keyboardType = UIKeyboardTypeNumberPad;
    [_textField becomeFirstResponder];
    _textField.textColor = RealBlackColor;
    _textField.tintColor = RealBlackColor;
    _textField.font = REAL_MEDIUM_FONT(16);
    _textField.delegate = self;
    [_textField addTarget:self action:@selector(textChangeEvent:) forControlEvents:UIControlEventEditingChanged];
    [inputView addSubview:_textField];
    
    _next = [[UIButton alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(inputView.frame)+20, SCREEN_WIDTH-60, 52)];
    _next.layer.masksToBounds = YES;
    [_next.layer setCornerRadius:18];
    _next.backgroundColor = GraryLightColor;
    _next.userInteractionEnabled = NO;
    [_next setTitle:@"下一步" forState:UIControlStateNormal];
    [_next setTitleColor:RealWhiteColor forState:UIControlStateNormal];
    [_next.titleLabel setFont:REAL_MEDIUM_FONT(16)];
    [self.view addSubview:_next];
    [[_next rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {

    
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        [param setValue:self.textField.text forKey:@"phonenumber"];
        CryptoHTTPKit *manager = [[CryptoHTTPKit alloc] init];
        [manager encryptedPOST:@"/auth/smslogin"
        parameters:param
        success:^(NSDictionary *responseObject) {
            if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
                VerCodeViewController *vc = [[VerCodeViewController alloc] init];
                vc.phonenumber = self.textField.text;
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                [[HCToast shareInstance] showToast:[NSString stringWithFormat:@"%@",responseObject[@"msg"]]];
            }
        }
        failure:^(NSError *aError) {
           
        }];
    }];
}

 -(void)textFieldDidBeginEditing:(UITextField *)textField
{
    _code.textColor = RealBlackColor;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    _code.textColor = ThemeDark2;
}

- (void)textChangeEvent:(UITextField *)textField
{
    if (textField.text.length>=11) {
        textField.text = [textField.text substringToIndex:11];
        _next.backgroundColor = NormalMainColor;
        _next.userInteractionEnabled = YES;
    }else{
        _next.backgroundColor = GraryLightColor;
        _next.userInteractionEnabled = NO;
    }
}

@end
